const timestampToDate = function(value, spe = '/') {
	value = value * 1000
	let data = new Date(value);
	let year = data.getFullYear();
	let month = data.getMonth() + 1;
	let day = data.getDate();
	let h = data.getHours();
	let mm = data.getMinutes();
	let s = data.getSeconds();
	month = month >= 10 ? month : "0" + month;
	day = day >= 10 ? day : "0" + day;
	h = h >= 10 ? h : "0" + h;
	mm = mm >= 10 ? mm : "0" + mm;
	s = s >= 10 ? s : "0" + s;
	return `${year}${spe}${month}${spe}${day} ${h}:${mm}:${s}`;
}


const getauthorize = function() {
	uni.authorize({
		scope: 'scope.userInfo',
		success() {
			uni.getUserInfo()
		}
	})
}

const getLocation = function() {
	uni.authorize({
		scope: 'scope.userLocation',
		success() {
			uni.getLocation()
		}
	})
}

const getUserInfos = function() {
	uni.authorize({
		scope: 'scope.userInfo',
		success() {
			uni.getUserInfo()
		}
	})
}

const getRandomString = function(len) {
	let str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
	let maxPos = str.length,
		rest = '';
	for (let i = 0; i < len; i++) {
		rest += str.charAt(Math.floor(Math.random() * maxPos));
	}
	return rest;
}

const getCode = function()
{
	uni.login({
		provider: 'weixin',
		success: res => {
			this.code = res.code;
			console.log('..................' + JSON.stringify(res) + '....' + this.code);
		}
	});
}


const getUserInfo = function() {
	uni.login({
		provider: 'weixin',
		scopes: 'auth_base',
		timeout: 20000,
		success: function(loginRes) {
			uni.setStorageSync('code', loginRes.code);
			uni.getUserInfo({
				success: function(infoRes) {
					uni.setStorageSync('userInfo', infoRes.userInfo);
				},
				fail: function(res) {
					// 这里res = {"errMsg":"getUserInfo:fail scope unauthorized"}   
					console.log('res=' + JSON.stringify(res))
					uni.setStorageSync('userInfo', '');
				}
			});
		}
	});
}

const showToast = function(title, image) {
	uni.showToast({
		title: title,
		image: image,
		duration: 2000
	})
}


const showModal = function(title, content, showCancel, cancelText, confirmText, surl, furl) {
	uni.showModal({
		title: title,
		content: content,
		showCancel: showCancel,
		cancelText: cancelText,
		confirmText: confirmText,
		success: function(res) {
			if (res.confirm) {
				uni.reLaunch({
					url: surl
				})
			} else if (res.cancel) {
				uni.reLaunch({
					url: furl
				})
			}
		}
	})
}

const getNetStatus = function() {
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType == 'none') {
				uni.showToast({
					title: '网络未连接',
					duration: 3000
				});
			}
		}
	});
}

const CurentTime = function() {
	var now = new Date();
	var year = now.getFullYear(); //年
	var month = now.getMonth() + 1; //月
	var day = now.getDate(); //日
	var hh = now.getHours(); //时
	var mm = now.getMinutes(); //分
	var ss = now.getSeconds(); //秒
	var clock = year + "-";
	if (month < 10) clock += "0";
	clock += month + "-";
	if (day < 10) clock += "0";
	clock += day + " ";
	if (hh < 10) clock += "0";
	clock += hh + ":";
	if (mm < 10) clock += '0';
	clock += mm + ":";
	if (ss < 10) clock += '0';
	clock += ss;
	return (clock);
}

const fmtTime = function() {
	var now = new Date();
	var year = now.getFullYear(); //年
	var month = now.getMonth() + 1; //月
	var day = now.getDate(); //日
	var hh = now.getHours(); //时
	var mm = now.getMinutes(); //分
	var ss = now.getSeconds(); //秒
	var clock = year + '';
	if (month < 10) clock += "0";
	clock += month + '';
	if (day < 10) clock += "0";
	clock += day + '';
	if (hh < 10) clock += "0";
	clock += hh + '';
	if (mm < 10) clock += '0';
	clock += mm + '';
	if (ss < 10) clock += '0';
	clock += ss + '';
	return (clock);
}



export default {
	getUserInfo,
	getauthorize,
	getLocation,
	showToast,
	getUserInfos,
	showModal,
	getNetStatus,
	getRandomString,
	CurentTime,
	fmtTime,
	timestampToDate
}